-- Billiards
-- Created by panyl Oct/27/2016
-- 通用赌桌

-- 初始化
function initBilliards(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 赌桌信息
    local gambleId = self.gridData.gamble_id;
    local gambleInfo = DungeonGambleM.query(gambleId);

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 领取奖励
    local function viewIronChest()
        DungeonActionM.go("do_building_bonus", self.gridData:getPos());

        EventMgr.fire(event.PLAYER_MOVE, self.gridData:getPos());
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("button_spell");

            if not DungeonM.isAllMonsterDead() then
                -- 获取赌博记录
                local record = DungeonGambleM.getGambleRecord();
                if record == nil then
                    -- 1.未赌过
                    -- 打开赌桌界面
                    local okFuncList = {};
                    local okTestList = {};

                    local options = DungeonGambleM.getGambleOptions(gambleId);
                    for i = 1, #options do
                        local optionId = options[i];
                        local optionInfo = DungeonGambleM.queryOption(optionId);
                        local function okFunc()
                            -- 计算扣除消耗
                            local allCost = DungeonGambleM.calcGambleOptionCost(optionId);
                            -- 消耗是否足够
                            if not DungeonGambleM.isEnoughToCost(allCost) then
                                if #allCost == 1 then
                                    local cost = allCost[1];
                                    local name;
                                    if cost[1] == 1 then
                                        name = ItemM.query(cost[2], "name");
                                    elseif cost[1] == 2 then
                                        name = FieldsM.getFieldName(cost[2]);
                                    end
                                    if name then
                                        -- 提示具体物品不足
                                        alert(string.format(getLocStr("something_not_enough"), name));
                                    else
                                        -- 直接提示材料不足！
                                        alert(getLocStr("forge_spade_material_lock"));
                                    end
                                else
                                    -- 直接提示材料不足！
                                    alert(getLocStr("forge_spade_material_lock"));
                                end
                                return;
                            end

                           DungeonActionM.go("normal_gamble", self.gridData:getPos(), optionId);
                        end
                        table.insert(okFuncList, okFunc);
                        table.insert(okTestList, optionInfo["name"]);
                    end

                    local offsetList = {30, 15, 20, 0, 0};
                    local para = {["long_btn_style"] = true};
                    self:openBuildingView5(okFuncList, okTestList, nil, gambleInfo["dialog"], nil, nil, offsetList, para);
                elseif record == 1 then
                    -- 2.赢了
                    self:openBuildingView(nil, 0, gambleInfo["success_dialog"], getLocStr("btn_text_leave"));
                elseif record == 0 then
                    -- 3.输了
                    self:openBuildingView(nil, 0, gambleInfo["fail_dialog"], getLocStr("btn_text_leave"));
                end

                return;
            end

            -- 打开建筑查看界面
            self:openBuildingView(viewIronChest);
        end
    end
    self.icon:addTouchEventListener(onClicked);
end
